// pages/a_oa/daka/daka.js

const app = getApp() // 获取应用实例
const util = require('../../../utils/util')
const uploadImage = require('../../../utils/upload');
import { getOaApi } from '../../../utils/http/api'

Page({

  // 页面的初始数据
  data: {
    // 用户列表 
    user_arr: [{ id: '537', name: '吴斌' }, { id: 544, name: '魏娟' }, { id: 539, name: '李功勋' }, { id: 540, name: '房咏锟' }, { id: 334, name: '陆安东' }],
    // 用户信息
    user: {},
    // 打卡信息
    daka: {},
    // 拍照上传成功的照片oss地址
    img_url: '',
    // 记录当前打卡地经纬度
    latlng: {},
    // 打卡ip地址池
    ip_arr: ['192.168.43.189', '192.168.110.163', '192.168.110.163', '10.138.122.155', '10.134.118.183', '10.76.78.89', '10.202.228.171'],
    // 打卡位置精度
    accuracy_arr: [0, 1.58, 5.23, 10.50, 18.46, 19.25, 20.12, 25.69, 26.82, 50.36, 67.03, 120.87,],
  },

  // 切换用户
  onSelUser(e) {
    let index = e.detail.value
    let uid = this.data.user_arr[index].id
    wx.setStorage({ key: 'uid', data: uid })// 切换用户后保存uid
    this.reqUserInfo(uid)
  },

  // 切换打卡地址
  onSelAddr(e) {
    this.setData({ latlng: this.data.daka.location[e.detail.value] })
  },

  onDakaClick(e) {
    let that = this
    wx.authorize({
      scope: 'scope.camera',
      success(res) {
        console.log("拍照授权...成功 = ", res);

        wx.chooseMedia({
          count: 1,
          sourceType: ['camera'],
          maxDuration: 10,
          camera: 'front',
          sizeType: ['compressed'],
          success(res) {
            console.log(res.tempFiles[0].tempFilePath)
            console.log((res.tempFiles[0].size / 1024.0).toFixed(2) + 'KB')
            that.uploadImg(res.tempFiles[0].tempFilePath)
          }
        })

      },
      fail(res) {
        console.log("拍照授权...失败 = ", res);
      },
    })

  },

  // 上传照片
  uploadImg(file) {
    let that = this
    wx.showLoading({ title: '上传中..', mask: true, })
    uploadImage(file, 'OA/upload/',// wuye/mp/ OA/upload/
      function (res) {
        wx.hideLoading()
        console.log('上传成功结果 = ', res);
        that.setData({ img_url: res })
        that.reqAddDaka()// 照片上传成功,请求添加打卡
      },
      function (res) {
        wx.hideLoading()
        console.log('上传失败。。', res);
      }
    )
  },

  // 请求OA用户信息
  async reqUserInfo(uid) {
    wx.showLoading({ title: '处理中', mask: true, })
    let { code, msg, data } = await getOaApi({ m: 'openAgentUser', a: 'userList', uid, })
    console.log(`reqUserInfo...code=${code},msg=${msg},data=`, data)
    wx.stopPullDownRefresh()
    if (code == '200') {
      this.setData({ user: data })
      this.reqDakaInfo(uid)
    } else {
      wx.showToast({ title: msg, icon: 'error' })
    }
  },

  // 请求考勤打卡信息
  async reqDakaInfo(id) {
    wx.showLoading({ title: '处理中', mask: true, })
    let { code, msg, data } = await getOaApi({ m: 'opendkq', a: 'getUserInfoData', id, })
    console.log(`reqDakaInfo...code=${code},msg=${msg},data=`, data)
    wx.stopPullDownRefresh()
    if (code == '200') {
      this.setData({ daka: data })
      this.setData({ latlng: this.data.daka.location[1] })
    } else {
      wx.showToast({ title: msg, icon: 'error' })
    }
  },

  // 新增打卡 
  async reqAddDaka() {
    wx.showLoading({ title: '处理中', mask: true, })
    let that = this
    let id = this.data.daka.id
    let lat = '35.112107'
    let lng = '118.282957'
    let sign_company = '顺和商贸集团'
    try {
      lat = this.data.latlng.lat
      lng = this.data.latlng.lng
      sign_company = this.data.latlng.rule_title
    } catch (error) { }
    let sign_time = util.formatTime3(new Date())
    let sign_img = this.data.img_url
    let address = `山东省临沂市兰山区${sign_company}`
    let ip = this.data.ip_arr[Math.floor((Math.random() * this.data.ip_arr.length))]
    let remark = ''
    let accuracy = this.data.accuracy_arr[Math.floor((Math.random() * this.data.accuracy_arr.length))]
    let waiqin = '2'
    let params = {
      m: 'opendkq', a: 'addkqdk', id, lat, lng, sign_company, sign_time, sign_img, address,
      ip, remark, accuracy, waiqin
    }
    console.log('打卡params = ', params);
    // return
    let { code, msg, data } = await getOaApi(params)
    console.log(`reqAddDaka...code=${code},msg=${msg},data=`, data)
    wx.stopPullDownRefresh()
    if (code == '200') {
      wx.showModal({
        title: '提示', content: '打卡成功', showCancel: false,
        success(res) {
          if (res.confirm) {// 点击确定
            that.getInfo()
          }
        }
      });
    } else {
      wx.showToast({ title: msg, icon: 'error' })
    }
  },

  // 生命周期函数--监听页面加载
  onLoad(options) {
    this.getInfo()
  },

  // 页面相关事件处理函数--监听用户下拉动作
  onPullDownRefresh() {
    this.getInfo()
  },

  getInfo() {
    let that = this
    wx.getStorage({
      key: 'uid',
      success(res) {
        that.reqUserInfo(res.data)
      },
      fail(res) {
        that.reqUserInfo('537')// 默认请求537用户
      }
    })
  },

  // 生命周期函数--监听页面显示
  onShow() {
  },

})